package com.watson.spring.all.security.service.security;

import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author watson
 * @date 2021/5/28 14:06
 * @Description: TODO
 **/
@Component
public class TokenManager {
    //token有效时间
    private long tokenEcpiration = 24 * 60 * 60 * 100;
    //编码密钥
    private String tokenSignkey = "123456";

    //使用jwt生成token
    public String createToken(String username) {
        String token = Jwts.builder().setSubject(username)
                .setExpiration(new Date(System.currentTimeMillis() + tokenEcpiration))
                .signWith(SignatureAlgorithm.HS256, tokenSignkey)
                .compressWith(CompressionCodecs.GZIP).compact();
        return token;
    }


    //根据token字符串获取用户信息
    public String getUserInfoForToken(String token) {
        String userInfo = Jwts.parser().setSigningKey(tokenSignkey)
                .parseClaimsJws(token)
                .getBody().getSubject();
        return userInfo;
    }

    //删除token
    public void removeToken(String token) {

    }

}
